package httprequest

import (
	"gitee.com/liukexing/mqbroker/core/driver/component"
	"gitee.com/liukexing/mqbroker/core/driver/config"
	"gitee.com/liukexing/mqbroker/core/server/pubsub"
	"gitee.com/liukexing/mqbroker/core/typedef"
	"github.com/robfig/cron"
	"time"
)

const (
	ConfigName = "config.component.system.httprequest"
)
var (
	Config   typedef.ConfigInfo
	Task   *cron.Cron
	LastTick time.Time
	Deamon typedef.SubHandle = Run
)

func init() {
	var ok bool
	Config, ok = config.GetConfig(ConfigName)
	if !ok {
		return
	}
	LastTick = time.Now().Add(-5 * time.Minute)
}

func Run() bool {
	if !Config.Info.Enable {
		return true
	}
	if pubsub.CommandMsgSubServer(Config.Info, NewCommandHandle()) {
		return true
	}
	if pubsub.CommandMsgSubClient(Config.Info, CommandMsgReceiver) {
		return true
	}

	if pubsub.ComputeMsgSubServer(Config.Info, NewComputeHandle()) {
		return true
	}
	if pubsub.ComputeMsgSubClient(Config.Info, ComputeMsgReceiver) {
		return true
	}

	if pubsub.MessageReplyServer(Config.Info, NewReplyHandle()) {
		return true
	}

	initMsgHandle()
	component.InitTask(Config.Info,Task,GetTaskInfo())
	return false
}